package com.example.demo.service;

import com.example.demo.untils.FileCheckerNIO;
import com.example.demo.untils.RtspCheckerUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component // 声明为 Spring 管理的单例 Bean
public class GlobalPropertyMonitor {

    @Autowired
    private FileBackupService fileBackupService;

    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private Object targetObject;
    private boolean isMonitoring = false;
    /////////-------------////////////

    /**
     * 设置监测目标对象
     */
    public void setTarget(Object target) {
        this.targetObject = target;
    }

    /**
     * 启动全局监测
     */
    public void startMonitoring() {
        if (!isMonitoring && targetObject != null) {
            isMonitoring = true;
            scheduler.scheduleAtFixedRate(() -> {
                boolean currentStatus = ((RecordInfo) targetObject).isStatus();
                String  rtspUrl=((RecordInfo) targetObject).getRtspUrl();
                // 同时检测连接状态:
//               boolean isAlive= RtspCheckerUtils.checkRtspStream(rtspUrl);
//               if(!isAlive){
//
//               }
                ///
                Date startTime=null;
                String sourceDir="D:\\jdk18\\hls2\\";
                String targetDir="D:\\jdk18\\hls2Record\\";
                if(currentStatus){
                    String id = ((RecordInfo) targetObject).getId();
                    //实时上传录制状态
                    Map<String,String> map=new HashMap();
                    map.put("macAddress",((RecordInfo) targetObject).getMacAddress());
                    map.put("businessId", ((RecordInfo) targetObject).getId());
                    map.put("businessType","1");
                    map.put("recordingStatus","1");

                     // 开始录制
                     startTime=((RecordInfo) targetObject).getStartTime();
                     Long fileName =((RecordInfo) targetObject).getFileName();
                     sourceDir=sourceDir+id;
                     targetDir=targetDir+id;
                     FileCheckerNIO.createDirectory(sourceDir);
                     FileCheckerNIO.createDirectory(targetDir);
                    try {
                        fileBackupService.backupFilesAfterDate(sourceDir,targetDir,startTime,fileName);
                    } catch (IOException e) {
                        e.printStackTrace();
                        map.put("recordingStatus","3");
                    }
                    RtspCheckerUtils.sendPostRequest(map);
                    /////////
                    System.out.println(id+"：：：id----[Monitor] Current status: " + currentStatus);
                }
            }, 0, 1, TimeUnit.SECONDS); // 每秒检查一次
        }
    }

    /**
     * 停止监测
     */
    public void stopMonitoring() {
        if (isMonitoring) {
            scheduler.shutdown();
            isMonitoring = false;
        }
    }

    /**
     * 强制终止
     */
    public void forceShutdown() {
        scheduler.shutdownNow();
        isMonitoring = false;
    }

     /////上传录制状态
}